package com.lyl.momanapp.frament;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.GridView;

import com.google.gson.Gson;
import com.lyl.momanapp.R;
import com.lyl.momanapp.adapter.Shop;
import com.lyl.momanapp.bean.ProductTemplates;
import com.lyl.momanapp.bean.URL;
import com.lyl.momanapp.util.utlis.MyUtils;
import com.lyl.momanapp.util.view.MyFragment;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 名字 梁亚龙
 * 时间 2017/10/13.
 * QQ邮箱 503934353@qq.com
 */

public class OneThree extends MyFragment {
    private GridView mGv;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case URL.ID_shop_liebiao: {
                    ProductTemplates productTemplates1 = new Gson().fromJson(msg.obj.toString(), ProductTemplates.class);
                    shop.showData((ArrayList<ProductTemplates.ResultBean.DataBean>) productTemplates1.getResult().getData());
//                    try {
//                        JSONArray jsonArray = new JSONArray(msg.obj.toString());
//                        ArrayList<ProductTemplates> list = new ArrayList<>();
//                        for (int i = 0; i <jsonArray.length() ; i++) {
//                            JSONObject json= (JSONObject) jsonArray.get(i);
//                            ProductTemplates productTemplates = new Gson().fromJson(jsonArray.get(i).toString(), ProductTemplates.class);
//                            if(i%2==0) {
//                                list.add(productTemplates);
//                            }
//                        }
//
//                        shop.showData(list);
//
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
                }
                break;
            }
        }
    };
    private Shop shop;

    @Override
    public void onClick(View v) {

    }

    @Override
    public int inflaterRootView() {
        return R.layout.frament_one_three;
    }

    @Override
    public void initUI() {
        mGv = findView(R.id.shop);
    }

    @Override
    public void initData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("user_id", "11074");
        map.put("per_page", "10");
        new MyUtils().xUtilsPost(URL.URL_shop_liebiao, map, handler, URL.ID_shop_liebiao);
        shop = new Shop(getContext());
        mGv.setAdapter(shop);
    }

    @Override
    public void addListeners() {
        shop.setOnItemClickLitener(new Shop.OnItemClickLitener() {
            @Override
            public void onItemClick(ProductTemplates.ResultBean.DataBean productTemplates) {
                URL.productTemplates = productTemplates;
            }
        });
    }
}
